package com.company.managesystem.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;

import com.company.managesystem.R;

/**
 * 启动页
 * Created by lqy on 2017/6/24.
 */

public class SplashActivity extends Activity {
    private static final int SHOW_TIME_MIN = 2000;// 最小显示时间
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isfirst();
            }
        } , SHOW_TIME_MIN);
    }

    private void isfirst(){
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo("com.company.managesystem",0);
        } catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
        }
        int currentVersion = info.versionCode;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        int lastVersion = preferences.getInt("VERSION_KEY" ,0);
        if (currentVersion > lastVersion) {
            //如果当前版本大于上次版本，该版本属于第一次启动
            //如果当前版本写入preference中，则下次启动的时候，据此判断，不再为首次启动
            Intent intent = new Intent(this,LoginActivity.class);
            startActivity(intent);
            finish();
            preferences.edit().putInt("VERSION_KEY",currentVersion).commit();
        } else {
            Intent intent = new Intent(this , LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
